//
//  OSSMAnager.swift
//  MM2
//
//  Created by 李成明 on 2022/1/2.
//

import Foundation
import UIKit
import AliyunOSSiOS

let ossBaseUrl = "https://mm-l-bucket.oss-cn-beijing.aliyuncs.com/"

class OSSManager {
    
    private init(){}
    
    static var share = OSSManager()
    
    var tAccessKey = "STS.NUvF25j7hSbH8Czs3vUoVgR3S"
    var tSecretKey = "AcR9oo9bnEE14Vn3Ae4Zwi18oN1XYr626qvvnUrNNS8"
    var tToken = "CAISmAJ1q6Ft5B2yfSjIr5bDDYiBh+hJ5KCjOmXLlzMjWeB6iJeYsTz2IHhIeHRuBeAbvvk0n2hS5vYclqJIRoReREvCUcZr8szbGPF+6c2T1fau5Jko1bejewHKeR+ZsebWZ+LmNpy/Ht6md1HDkAJq3LL+bk/Mdle5MJqP+/kFC9MMRVuAcCZhDtVbLRcY9618D3bKMuu3ORPHm3fZCFES2jBxkmRi86+ysL7+mxPVlw/90fRH5dazcJ2hKs0pNop0UNqpm65UDvOcj3MIsUEWrfws0PMYpWz904vDUwUL0XKvKPHJ99BjFgh9a5UhFrRMxPqGzqUk5rGNy9Sqm0sWZrkPA3SBXuK6yc7DA/i1O80lZQ1AteI2NV7lGoABcU1JId9FEWf6NUEH8Sk7r+YjlcrHRC31fgttl/0rN6metvyvPQxZrbRv94w0uLdYe5ixbfEzRu+RQVg0ZZEfLuVjkD2U2KRWolKbAIueEFqHPK40at8Ouuz/n8fqGYqN384qABCqZEzUPSGJu/d61iKkQzJ6pVlPen1eHRJbs+w="
 
    private var client: OSSClient?
    
    func uploadSingleImg(_ img: UIImage, path: String, completion: ((String) -> Void)?, isWaiting: Bool = true) {
        guard let imgdata = img.pngData() else { return }
        
        let putRequest = OSSPutObjectRequest()
        putRequest.bucketName = "mm-l-bucket"
        putRequest.objectKey = path
        putRequest.uploadingData = imgdata
        putRequest.uploadProgress = { bytesSent, totalByteSent, totalBytesExpectedToSend in
            print("bytesSent-->\(bytesSent), totalByteSent--->\(totalByteSent), totalBytesExpectedToSend--->\(totalBytesExpectedToSend)\n\n")
        }
        
        let task = client?.putObject(putRequest)
        if isWaiting {
            task?.continue({ (t) -> Any? in
                return nil
            }).waitUntilFinished()
        } else {
            task?.continue({ (t) -> Any? in
                return nil
            })
        }
        
        completion?(ossBaseUrl + path)
    }
    
    func uploadSingleImg(data: Data, path: String, completion: ((String) -> Void)?, isWaiting: Bool = true) {

        let putRequest = OSSPutObjectRequest()
        putRequest.bucketName = "mm-l-bucket"
        putRequest.objectKey = path
        putRequest.uploadingData = data
        putRequest.uploadProgress = { bytesSent, totalByteSent, totalBytesExpectedToSend in
            print("bytesSent-->\(bytesSent), totalByteSent--->\(totalByteSent), totalBytesExpectedToSend--->\(totalBytesExpectedToSend)\n\n")
        }
        
        let task = client?.putObject(putRequest)
        if isWaiting {
            task?.continue({ (t) -> Any? in
                return nil
            }).waitUntilFinished()
        } else {
            task?.continue({ (t) -> Any? in
                return nil
            })
        }
        completion?("https://mm-l-bucket.oss-cn-beijing.aliyuncs.com/\(path)")
    }
    
    func updateClient() {
        let ossProvider = OSSFederationCredentialProvider.init {
            let token = OSSFederationToken()
            token.tAccessKey = self.tAccessKey
            token.tToken = self.tToken
            token.tSecretKey = self.tSecretKey
//            token.tAccessKey = "STS.NTNPzaENBiFriGUw9MJ3ZGhfp"
//            token.tSecretKey = "6c9XbK4ugfS6uEPXe75h2t5auJvak5T7ka5rstcf4wfm"
//            token.tToken = "CAISmAJ1q6Ft5B2yfSjIr5f7G8DVqJFj3oSZa2HkkzkYRrx2qK3Nkjz2IHhIeHRuBeAbvvk0n2hS5vYclqJIRoReREvCUcZr8syPI/tM6M2T1fau5Jko1bejewHKeR+ZsebWZ+LmNpy/Ht6md1HDkAJq3LL+bk/Mdle5MJqP+/kFC9MMRVuAcCZhDtVbLRcY9618D3bKMuu3ORPHm3fZCFES2jBxkmRi86+ysL7+mxPVlw/90fRH5dazcJ2hKs0pNop0UNqpm65UDvOcj3MIsUEWrfws0PMYpWz904vDUwUL0XKvKPHJ99BjFgh9a5UhFrRMxPqGzqUk5rGNy9Sqm0sWZrkPA3SBXuK6yc7DA/i1O80lZQ1AteI2NV7lGoABE9c3LXkeRmkwYRAyHAzBYmLuh0+Abz9YkyZkT4Wfi2aIHGabmd0o5lWDjeGM++3AQGUTwWqJpOGoL7wH9bimbxkQQAoJDXy5rsq3P7xD7Q4RdqV6e9bu3YoonsJaUiDeJHi+xpHhLWFQxCxyiDBwbmbuUkmTkC4i37Q8vcDTU3o="
            return token
        }
        client = OSSClient.init(endpoint: "https://oss-cn-beijing.aliyuncs.com", credentialProvider: ossProvider)
    }
}
